import { queryCount, queryPage, groupListByToken, queryPageAll } from './service';
import { routerRedux } from 'dva/router';
import { queryDictList } from '@/services/dict';
import { DICT } from './enum';

export default {
  namespace: 'analyse_person',

  state: {
    count: {},
  },

  effects: {
    *queryCount(_, { call, put }) {
      const response = yield call(queryCount);
      yield put({ type: 'setCount', payload: response });
    },
    *updateCond({ payload }, { put }) {
      const { pageSize, pageNum, ...cond } = payload;
      yield put({ type: 'setCond', payload: cond });
      yield put({ type: 'queryPage', payload })
    },
    *queryPage({ payload = { pageNum: 1, pageSize: 10 }  }, { call, put, select }) {
      const cond = yield select(state => state.analyse_person.cond);
      const params = { ...payload, ...cond };
      yield put({ type: 'queryDict', payload: {keyList: DICT} });
      const { list = [], total } = yield call(queryPage, params);
      yield put({ type: 'setPage', payload: { list, total: +total } })
    },
    *queryPageAll(_, { call, put, select }) {
      const { list = [], total } = yield call(queryPageAll);
      yield put({ type: 'setPageAll', payload: { list, total: +total } })
    },
    *queryDictList({ payload }, { call, put }) {  // 下拉列表查
      const dictList = yield call(groupListByToken, payload);
      yield put({ type: 'setDictList', payload: dictList })
    },
    *queryDict({ payload }, { call, put }) {
      const dict = yield call(queryDictList, payload);
      yield put({ type: 'setDict', payload: dict })
    }
  },

  reducers: {
    setCond(state, action) {
      return {
        ...state,
        cond: action.payload
      }
    },
    setCount(state, action) {
      return {
        ...state,
        count: action.payload
      }
    },
    setPage(state, action) {
      return {
        ...state,
        list: {
          data: action.payload.list,
          dataCnt: action.payload.total
        }
      }
    },
    setPageAll(state, action) {
      return {
        ...state,
        listAll: {
          data: action.payload.list,
          dataCnt: action.payload.total
        }
      }
    },
    setDict(state, action) {
      return {
        ...state,
        dict: action.payload
      }
    },
    setDictList(state, action) {
      return {
        ...state,
        dictList: action.payload
      }
    }
  }
}
